<template>
  <div>
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker v-model="form.userDate1" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        userDate1: "",
        phone: "",
        address:"",
      },
      dialogVisibleState: true,
    };
  },
  props: ["dialogVisible"],
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.cancelAdd();
        })
        .catch((_) => {});
    },
    cancelAdd() {
      this.dialogVisibleState = false;
      this.$emit("changeVisible", this.dialogVisibleState);
    },
    addUser(){
        let name = this.form.name;
        let userDate1 = this.setDateTime();
        let phone = this.form.phone;
        let address = this.form.address;
        console.log(userDate1)
        let userObj = {
            userDate1,
            name,
            address,
            phone
        }
        this.$emit("addUserData", userObj);
    },
    setDateTime(){
      let DateString = this.form.userDate1.toLocaleDateString();
      let date = DateString.split('/');
      if(date[1]<10){
        date[1] = '0'+date[1]
      }
      if(date[2]<10){
        date[2] = '0'+date[2]
      }
      return date.join('-')
    }
  },
};
</script>

<style>
</style>